package com.weaver.qfengx.api.sf.shundiantong;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.weaver.qfengx.*;
import com.weaver.qfengx.api.sf.shundiantong.entity.CreateOrderDto;
import okhttp3.OkHttpClient;
import okhttp3.Response;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class ShundiantongApiService {

    private String appId;
    private String appSecret;
    private String host;
    private OkHttpClient client;
    private static final LogUtils log = new LogUtils(ShundiantongApiService.class);
    // 日志中间表
    private String logTable;



    public ShundiantongApiService(String appId, String appSecret, String host, OkHttpClient okHttpClient, String logTable) {
        this.appId = appId;
        this.appSecret = appSecret;
        this.host = host;
        this.client = okHttpClient;
        this.logTable = logTable;
    }

    public ShundiantongApiService(String appId, String appSecret, String host, String logTable) {
        this(appId, appSecret, host, new OkHttpClient.Builder().addInterceptor(new ReqLogInterceptor(logTable)).build(), logTable);
    }

    public ShundiantongApiService(String appId, String appSecret, String logTable) {
        this(appId, appSecret, "https://united-store-uat-ms.sf-express.com/zt/hd-united-store-openserver", logTable);
    }

    public ShundiantongApiService(String appId, String appSecret) {
        this(appId, appSecret, "https://united-store-uat-ms.sf-express.com/zt/hd-united-store-openserver", "");
    }




    public ShundiantongApiResult createOrder(CreateOrderDto createOrderDto) throws IOException {

        if (StringUtils.isEmpty(createOrderDto.getAppId())) {
            createOrderDto.setAppId(this.appId);
        }
        if (StringUtils.isEmpty(createOrderDto.getRequestId())) {
            createOrderDto.setRequestId(IDUtils.uuid());
        }
        if (StringUtils.isEmpty(createOrderDto.getTimestamp())) {
            createOrderDto.setTimestamp(DateUtils.timestamp());
        }
        List<String> params = Lists.newArrayList(
                createOrderDto.getAppId(), createOrderDto.getRequestId(), DateUtils.timestamp(), appSecret
        );
        if (StringUtils.isEmpty(createOrderDto.getSign())) {
            createOrderDto.setSign(ShundiantongUtils.sign(params));
        }
        Response response = RequestUtils.doPostByJson(this.client, this.host + ShundiantongConfig.createOrder , JSON.toJSONString(createOrderDto));
        String respBody = response.body().string();
        JSONObject res = JSONObject.parseObject(respBody);
        return ShundiantongApiResult.createResult(res.getString("errorCode"), res.getString("message"), response.code(), createOrderDto.getRequestId(), res.getJSONObject("data"));
    }

    // 取消订单
    public ShundiantongApiResult cancelOrder(String mailNumber) throws IOException {
        String requestId = IDUtils.uuid();
        String timestamp = DateUtils.timestamp();
        List<String> params = Lists.newArrayList(
                appId, requestId, timestamp, appSecret
        );
        String sign = ShundiantongUtils.sign(params);
        Map<String, Object> reqParam = Maps.newHashMap();
        reqParam.put("appId", appId);
        reqParam.put("timestamp", timestamp);
        reqParam.put("sign", sign);
        reqParam.put("mailNumber", mailNumber);
        reqParam.put("requestId", requestId);
        Response response = RequestUtils.doPostByJson(this.client, this.host + ShundiantongConfig.cancelOrder , JSON.toJSONString(reqParam));
        String respBody = response.body().string();
        JSONObject res = JSONObject.parseObject(respBody);
        return ShundiantongApiResult.createResult(res.getString("errorCode"), res.getString("message"), response.code(), requestId, res.getJSONObject("data"));

    }

    // 订单状态查询
    public ShundiantongApiResult searchOrder(String mailNumber) throws IOException {
        String requestId = IDUtils.uuid();
        String timestamp = DateUtils.timestamp();
        List<String> params = Lists.newArrayList(
                appId, requestId, timestamp, appSecret
        );
        String sign = ShundiantongUtils.sign(params);
        Map<String, Object> reqParam = Maps.newHashMap();
        reqParam.put("appId", appId);
        reqParam.put("timestamp", timestamp);
        reqParam.put("sign", sign);
        reqParam.put("mailNumber", mailNumber);
        reqParam.put("requestId", requestId);
        Response response = RequestUtils.doPostByJson(this.client, this.host + ShundiantongConfig.searchOrder , JSON.toJSONString(reqParam));
        String respBody = response.body().string();
        JSONObject res = JSONObject.parseObject(respBody);
        return ShundiantongApiResult.createResult(res.getString("errorCode"), res.getString("message"), response.code(), requestId, res.getJSONObject("data"));

    }

    public static void main(String[] args) throws IOException {
        ShundiantongApiService apiService = new ShundiantongApiService("154258", "82759c7d54b4d457");
//        CreateOrderDto createOrderDto = new CreateOrderDto();
//        createOrderDto.setCustomNumber("9999999999");
//        createOrderDto.setOrderNumber("TID" + IDUtils.snowFlakeId());
////        createOrderDto.setMailNumber("SF1126144360736");
//        createOrderDto.setCostType(CreateOrderCostType.CONSUMER);
//        createOrderDto.setSenderOrgCode("libangtouzi");
//        createOrderDto.setSenderName("测试");
//        createOrderDto.setReceiverName("收件人");
//        createOrderDto.setReceiverTel("18757432197");
//        createOrderDto.setSenderTel("18757432198");
//        createOrderDto.setReceiverAddress("收件地址");
//        createOrderDto.setSenderAddress("address");
//        createOrderDto.setPayMethod("1");
//        createOrderDto.setExpressType("6");
//        createOrderDto.setNeedReturnTrackingNumber("0");
//        CreateOrderCargoDto cargoDto = new CreateOrderCargoDto();
//        cargoDto.setAmount(null);
//        cargoDto.setWeight(null);
//        cargoDto.setUnit(null);
//        cargoDto.setName("测试物品");
//        cargoDto.setCount("1");
//        createOrderDto.setCargo(Lists.newArrayList(cargoDto));
////        apiService.createOrder(createOrderDto);
////        apiService.cancelOrder("SF1324439950472");
        System.out.println(apiService.searchOrder("496000074023"));


        System.out.println(0x3f3f3f3f);
    }

}
